package com.tensquare.friend.service;

import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.pojo.Friend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhc
 * @create 2019-04-18  14:56
 */
@Service
@Transactional
public class FriendService {
    @Autowired
    private FriendDao friendDao;

    public int addFriend(String userid,String friendid){
        //先判断是否重复添加数据
        Friend friend = friendDao.findByUseridAndAndFriendid(userid, friendid);
        if(friend!=null){
            return 0;
        }
        //直接添加好友
        friend=new Friend();
        friend.setUserid(userid);
        friend.setFriendid(friendid);
        friend.setIslike("0");
        friendDao.save(friend);
        //判断是否互相喜欢，如果是，将双方的type都修改为1
        Friend friendid1 = friendDao.findByUseridAndAndFriendid(friendid, userid);
        if(friendid1!=null){
            friendDao.updateIslike(userid,friendid,"1");
            friendDao.updateIslike(friendid,userid,"1");
        }
        return 1;
    }
}
